package com.itbaizhan.shopping_order_customer_api.controller;

/**
 * @author spf
 * @date 2024/1/19
 * @time 11:55
 */

import com.itbaizhan.shopping_common.pojo.Address;
import com.itbaizhan.shopping_common.pojo.Area;
import com.itbaizhan.shopping_common.pojo.City;
import com.itbaizhan.shopping_common.pojo.Province;
import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.AddressService;
import com.itbaizhan.shopping_common.utils.JWTUtil;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地址控制器API
 */
@RestController
@RequestMapping("/user/address")
public class AddressController {

    @Reference
    private AddressService addressService;

    /**
     * 获取所有省份
     * @return 省份下拉列表
     */
    @GetMapping("/findAllProvince")
    public BaseResult<List<Province>> findAllProvince(){
        List<Province> provinceList = addressService.findAllProvince();
        return BaseResult.ok(provinceList);
    }

    /**
     * 查询省份下的城市
     * @param provinceId 省份id
     * @return 城市列表
     */
    @GetMapping("/findCityByProvince")
    public BaseResult<List<City>> findCityByProvince(Long provinceId){
        List<City> cityList = addressService.findCityByProvince(provinceId);
        return BaseResult.ok(cityList);
    }

    /**
     * 查询城市下的区县
     * @param cityId 城市id
     * @return 区县列表
     */
    @GetMapping("/findAreaByCity")
    public BaseResult<List<Area>> findAreaByCity(Long cityId){
        List<Area> areaList = addressService.findAreaByCity(cityId);
        return BaseResult.ok(areaList);
    }

    /**
     * 添加地址
     * @param token 用户令牌
     * @param address 地址对象
     * @return 执行结果
     */
    @PostMapping("/add")
    public BaseResult add(@RequestHeader String token, @RequestBody Address address){
        Long userId = JWTUtil.getId(token);
        address.setUserId(userId);
        addressService.add(address);
        return BaseResult.ok();
    }

    /**
     * 跟更新地址
     * @param token 用户令牌
     * @param address 地址对象
     * @return 执行结果
     */
    @PutMapping("update")
    public BaseResult update(@RequestHeader String token, @RequestBody Address address){
        Long userId = JWTUtil.getId(token);
        address.setUserId(userId);
        addressService.update(address);
        return BaseResult.ok();
    }

    /**
     * 删除地址
     * @param id 地址id
     * @return 执行结果
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Long id){
        addressService.delete(id);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     * @param id 地址id
     * @return 地址
     */
    @GetMapping("/findById")
    public BaseResult<Address> findById(Long id){
        Address address = addressService.findById(id);
        return BaseResult.ok(address);
    }

    /**
     * 查询用户地址
     * @param token 用户令牌
     * @return 用户地址列表
     */
    @GetMapping("findByUser")
    public BaseResult<List<Address>> findByUser(@RequestHeader String token){
        Long userId = JWTUtil.getId(token);
        List<Address> addressList = addressService.findByUser(userId);
        return BaseResult.ok(addressList);
    }

}
